home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / xulrunner / python / BitTorrent / btformats.py < prev    next >
Encoding:
Python Source  |  2007-11-12  |  3.8 KB  |  102 lines

  1. # Written by Bram Cohen
  2. # see LICENSE.txt for license information
  3.  
  4. from types import StringType, LongType, IntType, ListType, DictType
  5. from re import compile
  6.  
  7. reg = compile(r'^[^/\\.~][^/\\]*$')
  8.  
  9. ints = (LongType, IntType)
  10.  
  11. def check_info(info):
  12.     if type(info) != DictType:
  13.         raise ValueError, 'bad metainfo - not a dictionary'
  14.     pieces = info.get('pieces')
  15.     if type(pieces) != StringType or len(pieces) % 20 != 0:
  16.         raise ValueError, 'bad metainfo - bad pieces key'
  17.     piecelength = info.get('piece length')
  18.     if type(piecelength) not in ints or piecelength <= 0:
  19.         raise ValueError, 'bad metainfo - illegal piece length'
  20.     name = info.get('name')
  21.     if type(name) != StringType:
  22.         raise ValueError, 'bad metainfo - bad name'
  23.     if not reg.match(name):
  24.         raise ValueError, 'name %s disallowed for security reasons' % name
  25.     if info.has_key('files') == info.has_key('length'):
  26.         raise ValueError, 'single/multiple file mix'
  27.     if info.has_key('length'):
  28.         length = info.get('length')
  29.         if type(length) not in ints or length < 0:
  30.             raise ValueError, 'bad metainfo - bad length'
  31.     else:
  32.         files = info.get('files')
  33.         if type(files) != ListType:
  34.             raise ValueError
  35.         for f in files:
  36.             if type(f) != DictType:
  37.                 raise ValueError, 'bad metainfo - bad file value'
  38.             length = f.get('length')
  39.             if type(length) not in ints or length < 0:
  40.                 raise ValueError, 'bad metainfo - bad length'
  41.             path = f.get('path')
  42.             if type(path) != ListType or path == []:
  43.                 raise ValueError, 'bad metainfo - bad path'
  44.             for p in path:
  45.                 if type(p) != StringType:
  46.                     raise ValueError, 'bad metainfo - bad path dir'
  47.                 if not reg.match(p):
  48.                     raise ValueError, 'path %s disallowed for security reasons' % p
  49.         for i in xrange(len(files)):
  50.             for j in xrange(i):
  51.                 if files[i]['path'] == files[j]['path']:
  52.                     raise ValueError, 'bad metainfo - duplicate path'
  53.  
  54. def check_message(message):
  55.     if type(message) != DictType:
  56.         raise ValueError
  57.     check_info(message.get('info'))
  58.     announce = message.get('announce')
  59.     if type(announce) != StringType or len(announce) == 0:
  60.         raise ValueError, 'bad torrent file - announce is invalid'
  61.  
  62. def check_peers(message):
  63.     if type(message) != DictType:
  64.         raise ValueError
  65.     if message.has_key('failure reason'):
  66.         if type(message['failure reason']) != StringType:
  67.             raise ValueError
  68.         return
  69.     peers = message.get('peers')
  70.     if type(peers) == ListType:
  71.         for p in peers:
  72.             if type(p) != DictType:
  73.                 raise ValueError
  74.             if type(p.get('ip')) != StringType:
  75.                 raise ValueError
  76.             port = p.get('port')
  77.             if type(port) not in ints or p <= 0:
  78.                 raise ValueError
  79.             if p.has_key('peer id'):
  80.                 id = p.get('peer id')
  81.                 if type(id) != StringType or len(id) != 20:
  82.                     raise ValueError
  83.     elif type(peers) != StringType or len(peers) % 6 != 0:
  84.         raise ValueError
  85.     interval = message.get('interval', 1)
  86.     if type(interval) not in ints or interval <= 0:
  87.         raise ValueError
  88.     minint = message.get('min interval', 1)
  89.     if type(minint) not in ints or minint <= 0:
  90.         raise ValueError
  91.     if type(message.get('tracker id', '')) != StringType:
  92.         raise ValueError
  93.     npeers = message.get('num peers', 0)
  94.     if type(npeers) not in ints or npeers < 0:
  95.         raise ValueError
  96.     dpeers = message.get('done peers', 0)
  97.     if type(dpeers) not in ints or dpeers < 0:
  98.         raise ValueError
  99.     last = message.get('last', 0)
  100.     if type(last) not in ints or last < 0:
  101.         raise ValueError
  102.